package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Orders;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 分页查询和条件查询
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findByPage(PageParam pageParam) {
        Page<Orders> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        //分页查询
        if (pageParam.getNumber() == null && pageParam.getBeginTime() == null && pageParam.getEndTime() == null) {
            return R.success(orderService.page(page));
        }

        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        //模糊查寻
        qw.like( Orders::getNumber,pageParam.getNumber());

        //条件查询
        qw.between(pageParam.getBeginTime()!=null||pageParam.getEndTime()!=null,Orders::getOrderTime,pageParam.getBeginTime(),pageParam.getEndTime());



        //调用service
        Page<Orders> ordersPage = orderService.page(page, qw);
        //判断结果
        if (ordersPage!=null){
            if (ordersPage.getSize()>0){
                return R.success(ordersPage);
            }
        }
        return R.error("查询失败");

    }
    /**
     * 修改订单的状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders) {
        if(orderService.updateById(orders)){
            return R.success("该订单已经修改成了派送状态");
        }
        return R.error("该订单已经是派送状态,无需修改");
    }

    }
import com.itheima.bean.Orders;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 提交订单
     * @param order
     * @return
     */
    @PostMapping("/order/submit")
    public R submitOrder(@RequestBody Orders order){

        int row = orderService.add(order);

        if(row > 0 ){

            shoppingCartService.clean();

            return R.success("提交订单成功！");
        }
        return R.success("提交订单失败！");

    }
}
